package net.sudot.chess.plugin.login;


import net.sudot.chess.business.model.PluginConfig;
import net.sudot.chess.business.model.SocialUser;
import net.sudot.chess.plugin.BasePlugin;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.util.SavedRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录插件
 *
 * @author tangjialin on 2018-03-18.
 */
public abstract class LoginPlugin extends BasePlugin {
    /** "LOGO"属性名称 */
    public static final String LOGO_ATTRIBUTE_NAME = "logo";

    /** "描述"属性名称 */
    public static final String DESCRIPTION_ATTRIBUTE_NAME = "description";

    /** 默认登录视图名称 */
    public static final String DEFAULT_LOGIN_VIEW = "/social-user-login/login";

    /**
     * 获取LOGO
     *
     * @return LOGO
     */
    public String getLogo() {
        PluginConfig pluginConfig = getConfig();
        return pluginConfig != null ? pluginConfig.getAttribute(LOGO_ATTRIBUTE_NAME) : null;
    }

    /**
     * 获取描述
     *
     * @return 描述
     */
    public String getDescription() {
        PluginConfig pluginConfig = getConfig();
        return pluginConfig != null ? pluginConfig.getAttribute(DESCRIPTION_ATTRIBUTE_NAME) : null;
    }

    /**
     * 登录前处理
     *
     * @param extra        附加内容
     * @param request      HttpServletRequest
     * @param response     HttpServletResponse
     * @param modelAndView ModelAndView
     */
    public void loginBeforeHandle(String extra, HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        modelAndView.setViewName("redirect:" + getDoLoginUrl(extra));
    }

    /**
     * 登录处理
     *
     * @param extra        附加内容
     * @param request      HttpServletRequest
     * @param response     HttpServletResponse
     * @param modelAndView ModelAndView
     */
    public abstract void loginHandle(String extra, HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView);

    /**
     * 登录后处理
     *
     * @param socialUser     社会化用户
     * @param extra          附加内容
     * @param isLoginSuccess 是否登录成功
     * @param request        HttpServletRequest
     * @param response       HttpServletResponse
     * @param modelAndView   ModelAndView
     */
    public void loginAfterHandle(SocialUser socialUser, String extra, boolean isLoginSuccess, HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        if (socialUser != null && socialUser.getUserId() != null) {
            String redirect;
            SavedRequest savedRequest = org.apache.shiro.web.util.WebUtils.getAndClearSavedRequest(request);
            String contextPath = request.getContextPath();
            if (savedRequest != null) {
                redirect = StringUtils.substringAfter(savedRequest.getRequestUrl(), contextPath);
            } else {
                String sessionRedirect = (String) request.getSession().getAttribute(HttpHeaders.LOCATION);
                if (sessionRedirect != null) {
                    redirect = sessionRedirect;
                    request.getSession().removeAttribute(HttpHeaders.LOCATION);
                } else {
                    redirect = null;
                }
            }
            modelAndView.setViewName("redirect:" + redirect);
        } else {
            modelAndView.setViewName("redirect:" + null);
        }
    }

    /**
     * 判断是否登录成功
     *
     * @param extra    附加内容
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @return 是否登录成功
     */
    public abstract boolean isLoginSuccess(String extra, HttpServletRequest request, HttpServletResponse response);

    /**
     * 获取登录前处理URL
     *
     * @param extra 附加内容
     * @return 登录前处理URL
     */
    public String getLoginBeforeUrl(String extra) {
        String url = systemProperties.getWebsite() + "/login/social-user/before/" + getId();
        return extra == null || extra.isEmpty() ? url : url + "/" + extra;
    }

    /**
     * 获取登录处理URL
     *
     * @param extra 附加内容
     * @return 登录处理URL
     */
    public String getDoLoginUrl(String extra) {
        String url = systemProperties.getWebsite() + "/login/social-user/do/" + getId();
        return extra == null || extra.isEmpty() ? url : url + "/" + extra;
    }

    /**
     * 获取登录后处理URL
     *
     * @param extra 附加内容
     * @return 登录后处理URL
     */
    public String getLoginAfterUrl(String extra) {
        String url = systemProperties.getWebsite() + "/login/social-user/after/" + getId();
        return extra == null || extra.isEmpty() ? url : url + "/" + extra;
    }

    /**
     * 获取唯一ID
     *
     * @param request HttpServletRequest
     * @return 唯一ID
     */
    public abstract String getUniqueId(HttpServletRequest request);

}